var common = require("common");
cc.Class({
    extends: cc.Component,

    properties: {
        id:cc.Label,
        setBtn:cc.Button,
        shezhiBtn:cc.Button,
        guanbisetBtn:cc.Button, //设置下的小窗口按钮
        gengduoBtn:cc.Button,  
        kefuBtn:cc.Button,
        shengciBtn:cc.Button,
        guanshengyinBtn:cc.Button,
        kaishengyinBtn:cc.Button,
        shengciben:cc.Node,
        guanbiscBtn:cc.Button,  //生词本下的小窗口按钮

        freeBtn:cc.Button,
        mianfeitili:cc.Node,
        guanbitiBtn:cc.Button,  //免费体力下的小窗口按钮
        shiwanBtn:cc.Button,
        huihuaBtn:cc.Button,
        gongzhongBtn:cc.Button,
        haoyouBtn:cc.Button,

        rankBtn:cc.Button,
        paihangbang:cc.Node,
        guanbiRankBtn:cc.Button,  //排行榜下的小窗口按钮
        my:cc.Node,
        rankscroll:cc.ScrollView,
        weixinBtn:cc.Button,
        allBtn:cc.Button,

        tili:cc.Node,
        tiliwenben:cc.Label,
        tilizhi:cc.Label,
        
        dianguBtn:cc.Button,
        diangushouji:cc.Node,  //典故收集下的小窗口按钮
        guanbidgBtn:cc.Button,
        dianguscroll:cc.ScrollView,
        
        tiaozhanBtn:cc.Button,
        meiritiaozhan:cc.Node,   //每日挑战下的小窗口按钮
        guanbitzBtn:cc.Button,
        kaishiBtn:cc.Button,
        chaungjianBtn:cc.Button,

        playerBtn:cc.Button,
        playerup:cc.Node,     //人物界面
        player:cc.Node,
        chenghaoBtn:cc.Button,
        chenghaoLabel:cc.Label,
        guanbiplayerBtn:cc.Button,
        playerscroll:cc.ScrollView,

        houseBtn:cc.Button,   //房子
        house:cc.Node,
        maiwuBtn:cc.Button,
        maiwuzhidi:cc.Node,
        guanbimwBtn:cc.Button,
        housescroll:cc.ScrollView,

        tipLabel:cc.Label,
        datiBtn:cc.Button,
        zhaoxian:cc.Button,
        jinsheng:cc.Button,

        interval:0,
        array:[],
        brr:[],
        crr:[],
        crray:[],
        game:cc.Node,
    },
    onLoad () 
    {
        this.setBtn.node.on("click",this.OnSetBtn,this);
        this.shezhiBtn.node.on("click",this.OnSheZhiBtn,this);
        this.guanbisetBtn.node.on("click",this.OnGuanBiSetBtn,this);
        this.gengduoBtn.node.on("click",this.OnGenDuoBtn,this);
        this.kefuBtn.node.on("click",this.OnKeFuBtn,this);
        this.guanshengyinBtn.node.on("click",this.OnGuanSYBtn,this);
        this.kaishengyinBtn.node.on("click",this.OnKaiSYBtn,this);
        this.shengciBtn.node.on("click",this.OnShengCiBtn,this);
        this.guanbiscBtn.node.on("click",this.OnGuanBiSCBtn,this);
        this.freeBtn.node.on("click",this.OnFreeBtn,this);
        this.guanbitiBtn.node.on("click",this.OnGuanBiTiBtn,this);
        this.shiwanBtn.node.on("click",this.OnShiWanBtn,this);
        this.huihuaBtn.node.on("click",this.OnHuiHuaBtn,this);
        this.gongzhongBtn.node.on("click",this.OnGongZhongBtn,this);
        this.haoyouBtn.node.on("click",this.OnHaoYouBtn,this);
        this.rankBtn.node.on("click",this.OnRankBtn,this);
        this.guanbiRankBtn.node.on("click",this.OnGuanBiRankBtn,this);
        
        this.dianguBtn.node.on("click",this.OnDianGuBtn,this);
        this.guanbidgBtn.node.on("click",this.OnGuanBiDGBtn,this);
        this.tiaozhanBtn.node.on("click",this.OnTiaoZhanBtn,this);
        this.guanbitzBtn.node.on("click",this.OnGuanBiTZBtn,this);
        this.playerBtn.node.on("click",this.OnPlayerBtn,this);
        this.chenghaoBtn.node.on("click",this.OnChengHaoBtn,this);
        this.guanbiplayerBtn.node.on("click",this.OnGuanBiPlayerBtn,this);
        this.houseBtn.node.on("click",this.OnHouseBtn,this);
        this.maiwuBtn.node.on("click",this.OnMaiWuBtn,this);
        this.guanbimwBtn.node.on("click",this.OnGuanBiMWBtn,this);
        this.datiBtn.node.on("click",this.OnDaTiBtn,this);

        this.shengciben = cc.find("Canvas/Hall/shengciben");
        this.mianfeitili = cc.find("Canvas/Hall/mianfeitili");
        this.paihangbang = cc.find("Canvas/Hall/paihangbang");
        this.diangushouji = cc.find("Canvas/Hall/diangushouji");
        this.meiritiaozhan = cc.find("Canvas/Hall/meiritiaozhan");
        this.playerup = cc.find("Canvas/Hall/playerup");
        this.maiwuzhidi = cc.find("Canvas/Hall/maiwuzhidi");
        this.my = cc.find("Canvas/Hall/paihangbang/my");
        this.zhaoxian.node.on("click",this.OnZhaoXianBtn,this);
        this.jinsheng.node.on("click",this.OnJinShengBtn,this);

        common.tilitubiao = cc.find("dibu/dati/tilitubiao",this.node);

        var self = this;
        //加载人物升级文件
        cc.loader.loadRes("peizhi/player",function(error,PlayerTxt) {
            self.playergrade = PlayerTxt.text;
            self.node.emit("LoadPlayer",{});
        });
        this.node.on("LoadPlayer",function() {
            this.ChaiPlayer();
        },this);
        //加载房屋升级文件
        cc.loader.loadRes("peizhi/house",function(error,HouseTxt) {
            self.housegrade = HouseTxt.text;
            self.node.emit("LoadHouse",{});
        });
        this.node.on("LoadHouse",function() {
            this.ChaiHouse();
        },this);
        //检测人物是否达到升级
        for (let i = 0; i < this.array.length; i++) {
            if (common.level == this.array[i]) {
                //可以升级
                this.jinsheng.active = true;
            }
        }
        //检测房屋是否达到升级
        for (let i = 0; i < this.crray.length; i++) {
            if (common.level == this.crray[i]) {
                //可以升级
                this.maiwuBtn.active = true;
            }
        }
    },
    ChaiPlayer(){
        let arr = this.playergrade.split("\n");
        for (let i = 1; i < arr.length; i++) {
            this.brr.push(arr[i]);
        }
        for (let i = 0; i < this.brr.length; i++) {
            let num = (this.brr[i].split("	"))[4];
            this.array.push(num);
        }
    },
    ChaiHouse(){
        let arr = this.housegrade.split("\n");
        for (let i = 1; i < arr.length; i++) {
            this.crr.push(arr[i]);
        }
        for (let i = 0; i < this.crr.length; i++) {
            let num = (this.crr[i].split("	"))[2];
            this.crray.push(num);
        }
    },
    OnSetBtn:function(event)
    {
        this.shezhiBtn.node.active = true;
    },
    OnSheZhiBtn:function(event) 
    {
        this.shezhiBtn.node.active = false;
    },
    OnGuanBiSetBtn:function(event) 
    {
        this.shezhiBtn.node.active = false;
    },
    OnGenDuoBtn:function(event) 
    {
        
    },
    OnKeFuBtn:function(event) 
    {
        
    },
    OnShengCiBtn:function(event) 
    {
        this.shengciben.active = true;
    },
    OnGuanBiSCBtn:function(event) 
    {
        this.shengciben.active = false;
    },
    OnGuanSYBtn:function(event) 
    {
        this.guanshengyinBtn.node.active = false;
        this.kaishengyinBtn.node.active = true;
        common.shengyin.volume = 0;
    },
    OnKaiSYBtn:function(event) 
    {
        this.guanshengyinBtn.node.active = true;
        this.kaishengyinBtn.node.active = false;
        common.shengyin.volume = 1;
    },
    OnFreeBtn:function(event) 
    {
        this.mianfeitili.active = true;
    },
    OnGuanBiTiBtn:function(event) 
    {
        this.mianfeitili.active = false;
    },
    OnShiWanBtn:function(event) 
    {
        //跳转游戏界面
    },
    OnHuiHuaBtn:function(event) 
    {

    },
    OnGongZhongBtn:function(event) 
    {

    },
    OnHaoYouBtn:function(event) 
    {

    },
    OnRankBtn:function(event) 
    {
        this.paihangbang.active = true;
    },
    OnGuanBiRankBtn:function(event) 
    {
        this.paihangbang.active = false;
    },
    OnDianGuBtn:function(event) 
    {
        this.diangushouji.active = true;
    },
    OnGuanBiDGBtn:function(event) 
    {
        this.diangushouji.active = false;
    },
    OnTiaoZhanBtn:function(event) 
    {
        this.meiritiaozhan.active = true;
    },
    OnGuanBiTZBtn:function(event) 
    {
        this.meiritiaozhan.active = false;
    },
    OnPlayerBtn:function(event) 
    {
        this.playerup.active = true;
    },
    OnChengHaoBtn:function(event) 
    {
        this.playerup.active = true;
    },
    OnGuanBiPlayerBtn:function(event) 
    {
        this.playerup.active = false;
    },
    OnHouseBtn:function(event) 
    {
        this.maiwuzhidi.active = true;
    },
    //房屋变换
    OnMaiWuBtn:function(event) 
    {
        var body;
        for (let i = 0; i < this.crr.length; i++) {
            let str = this.crr[i].split("	");
            if (common.level == str[2]) {
                body = str[1];
            }
        }
        //加载房屋图片
        var self = this;
        cc.loader.loadRes("img/house/" + body,cc.SpriteFrame,function(err,spriteFrame1) {
            cc.find("bady",self.house).getComponent(cc.Sprite).spriteFrame = spriteFrame1;
        });
    },
    OnGuanBiMWBtn:function(event) 
    {
        this.maiwuzhidi.active = false;
    },
    OnDaTiBtn:function(event) 
    {
        this.UseTili();
        common.tilitubiao.active = false;
        //跳转游戏界面
        this.game.active = true;
        // cc.director.loadScene("Game");
    },
    UseTili(){
        if (common.tilitubiao.active) {
            common.tilishu -= 1;
        }
    },
    OnZhaoXianBtn:function(event) 
    {
        
    },
    //晋升变装
    OnJinShengBtn:function(event) 
    {
        var head;
        var body;
        for (let i = 0; i < this.brr.length; i++) {
            let str = this.brr[i].split("	");
            if (common.level == str[4]) {
                head = str[1];
                body = str[2];
                this.chenghaoLabel.string = str[3];
            }
        }
        //加载人物图片
        var self = this;
        cc.loader.loadRes("img/player/" + head,cc.SpriteFrame,function(err,spriteFrame1) {
            cc.find("bady/head",self.player).getComponent(cc.Sprite).spriteFrame = spriteFrame1;
        });
        cc.loader.loadRes("img/player/" + body,cc.SpriteFrame,function(err,spriteFrame2) {
            cc.find("bady",self.player).getComponent(cc.Sprite).spriteFrame = spriteFrame2;
        });
    },
    // start () { },
    update (dt) 
    {
        this.tilizhi.string = "" + common.tilishu;
        if (common.tilishu == 0) {
            cc.log("体力不足");
        }
        if (common.tilishu >= 5) {
            this.tiliwenben.string = "已满";
            common.tilitubiao.active = true;
        }else if(common.tilishu < 5){
            this.interval = this.interval + dt;             
            if (common.TiliTime > 0) 
            {
                if (this.interval >= 1) 
                {
                    setTimeout(function(){
                        common.TiliTime -= 1;
                    },1);
                    this.interval = 0;
                }
            }else{
                common.tilishu += 1;
                this.tilizhi.string = "" + common.tilishu;
            }
            this.tiliwenben.string = "" +  Math.floor(common.TiliTime / 60) + ":" + (common.TiliTime % 60);
        }
    },
});
